package cate.game.res.hoard;

import cate.common.table.d.GDFunc;
import cate.common.table.d.GDObj;
import cate.common.table.item.PropPartRow;
import cate.common.table.res.hoard.row.HoardComposeRow;
import cate.common.table.res.hoard.row.HoardTypeRow;
import cate.common.util.GameResult;
import cate.game.res.MixRes;
import cate.game.res.MixResItem;
import cate.game.res.hoard.addition.HoardAddition;
import cate.game.res.hoard.msg.HoardInfoResp;
import cate.game.role.FuncOpenChecker;
import cate.game.role.Role;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;

public class Hoard extends FuncOpenChecker {
    @NoteField(value = "秘藏经验")
    public int hoardExp;

    @NoteField(value = "图鉴<原型ID，当前星级>")
    public Map<Integer, Integer> gotMap;

    @NoteField("加成")
    public HoardAddition addition;

    @Override
    public void initialize(Role role) {
        super.initialize(role);
        if (gotMap == null) {
            gotMap = new HashMap<>();
        }
        if (addition == null) {
            addition = new HoardAddition();
        }
        addition.initialize(role);
    }

    @Override
    public int getFuncId() {
        return GDFunc.HOARD;
    }

    @Override
    public void onAfterOpen() {
        noticeUpdate();
    }

    public void noticeUpdate() {
        if (open) {
            role.sendNow(new HoardInfoResp(this));
        }
    }

    public GameResult<Void> compose(int tid) {
        GameResult<Void> r = new GameResult<>();
        //未合成
        int star = 0;
        HoardComposeRow composeRow = role.getGame().table.hoard.compose.showCompose(tid);
        if (composeRow == null) {
            return r.fail("合成配置错误");
        }
        MixRes cost = new MixRes(composeRow.costStr);
        HoardTypeRow hoardTypeRow = role.getGame().table.hoard.hoardType.showHoard(tid, star);
        if (hoardTypeRow == null) {
            return r.fail("配置错误");
        }
        HoardItem item = new HoardItem(tid, hoardTypeRow.taskId, hoardTypeRow.numMax);
        //升星
        if (gotMap.containsKey(tid)) {
            hoardTypeRow = role.getGame().table.hoard.hoardType.showHoard(tid, gotMap.get(tid));
            if (hoardTypeRow == null) {
                return r.fail("升星配置错误");
            }
            if (!hoardTypeRow.consumeStr.isEmpty()) {
                star = hoardTypeRow.star + 1;
            } else {
                return r.fail("已升到最高");
            }
            item = addition.itemMap.get(tid);
            cost = new MixRes(hoardTypeRow.consumeStr);

        }

        //碎片消耗check
        GameResult<MixResItem> consume = cost.consumeCheck(role);
        if (!consume.ok()) {
            return r.fail(consume);
        }

        //碎片消耗
        cost.consume(role);
        //秘藏星级更新
        gotMap.put(tid, star);
        HoardTypeRow hoardNew = role.getGame().table.hoard.hoardType.showHoard(tid, star);
        item.itemId = hoardNew.id;
        item.taskCountMax = hoardNew.numMax;
        addition.itemMap.put(tid, item);
        //增加经验
        hoardExp += hoardNew.hoardExp;
        noticeUpdate();
        return r;
    }
}
